using NUnit.Framework;
using Tycho.Lexer;
using Tycho.Parser.Peg;
using Tycho.Parser.Tokens;

namespace Tycho.Parser.Tests.Peg {
    [TestFixture]
    public class NamedRuleSpecification {
        [Test]
        public void ShouldAddResultOfProductionParseIfSuccessfullyParsed () {
            NamedRule<object> rule = new NamedRule<object> ("a", new IdentifierTerminal<object> ());

            Token [] tokens = NativeLexer.Lex ("let").ToArray ();

            RuleParseResult<object> result = rule.Parse (tokens, 0, null);
            Assert.IsNotNull (result);

            Assert.AreEqual (1, result.Captures.Count);
            result.AssertCaptureIs (0, "a", "let");
        }

        [Test]
        public void ShouldNotAddResultIfNotParsed () {
            NamedRule<object> rule = new NamedRule<object> ("a", new IdentifierTerminal<object> ());

            Token [] tokens = NativeLexer.Lex ("9").ToArray ();

            Assert.IsNull (rule.Parse (tokens, 0, null));
        }
    }
}